package com.elite.groovy.dev;

import com.elite.groovy.kh.KHUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.dev
 * @className: soc
 * @author: LiJT
 * @description: TODO
 * @date: 2024-04-01 16:23
 * @version: 1.0
 */
public class WebSocketClientFactory_Test {

    @Autowired
    private static WebSocketClientFactory webSocketClientFactory = new WebSocketClientFactory();

    public static void main(String[] args) {
        // 项目启动的时候打开websocket连接
        webSocketClientFactory.retryOutCallWebSocketClient();
        String socketMsg = "{\"source\":\"000001\",\"target\":\"000001\",\"targetType\":0,\"clientType\":\"pc\",\"message\":\"" + KHUtil.getRandomString(4) + "\",\"refid\":\"" + KHUtil.getRandomString(36) + "\",\"module\":\"\"}";

        JSONObject socketJson = JSONObject.fromObject(socketMsg);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Boolean result = false;
        int count = 0;
        while (!result) {
            try {
                Thread.sleep(5 * 1000); //设置暂停的时间 5 秒
                count++;
                System.out.println(sdf.format(new Date()) + "--循环执行第" + count + "次");
                socketJson.put("module", count);
                webSocketClientFactory.sendMsg(webSocketClientFactory.getOutCallWebSocketClientHolder(), socketJson.toString());
                if (count == 100) {
                    result = true;
                    break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
